package com.atguigu.stgy.admin.controller;

import com.atguigu.stgy.admin.service.SystemUserService;
import com.atguigu.stgy.model.dto.SystemUserSearchDTO;
import com.atguigu.stgy.model.entity.SystemUser;
import com.atguigu.stgy.model.result.R;
import com.atguigu.stgy.model.vo.SystemUserVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * 员工信息表(system_user)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {
    /**
     * 服务对象
     */
    @Resource
    private SystemUserService systemUserService;
    @Operation(summary = "新增管理员")
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody SystemUser systemUser) {
        systemUserService.saveSystemUser(systemUser);
        return R.ok();
    }

    //isUserNameAvailable?username=qqqq
    @Operation(summary = "判断用户名是否存在")
    @GetMapping("isUserNameAvailable")
    public R<Boolean> isUserNameAvailable(@RequestParam String username) {
        //数据库表使用了逻辑删除：如果创建的用户被删除了，校验不能查询被删除的用户  select count(*) from system_user where username = xx and is_deleted = 0
        //如果新增的用户名 和已删除的用户名一样 仍然可以使用   (数据库表如果设置了唯一约束 会出现数据库异常)
        Boolean flag = systemUserService.count(Wrappers.lambdaQuery(SystemUser.class)
                .eq(SystemUser::getUsername , username))==0;

        return R.ok(flag);
    }

    ///admin/system/user/page?current=1&size=10&name=13123&phone=123123
    @Operation(summary = "条件查询系统管理员分页数据")
    @GetMapping("page")
    public R<Page<SystemUserVO>> pageItem(SystemUserSearchDTO searchDTO) {
        Page<SystemUserVO> page = systemUserService.pageItem(searchDTO);

        return R.ok(page);
    }
}
